package ms.shop.service;

import com.github.pagehelper.PageHelper;
import ms.common.exception.BizException;
import ms.common.page.Page;
import ms.common.util.bean.BeanUtil;
import ms.common.util.string.StringUtil;
import ms.shop.bean.CommonQueryParamBean;
import ms.shop.bean.SkuParamBean;
import ms.shop.config.ResponseCode;
import ms.shop.dao.SkuAttrDAO;
import ms.shop.dao.SkuDAO;
import ms.shop.dao.SkuImgDAO;
import ms.shop.entity.Attr;
import ms.shop.entity.Sku;
import ms.shop.entity.SkuAttr;
import ms.shop.entity.SkuImg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Project Name: ms
 * File Name: SkuService
 * Package Name: ms.shop.service
 * Date: 2018/6/4  10:30
 * Copyright (c) 2018, tianyul All Rights Reserved.
 */
@Service
public class SkuService {

    @Autowired
    private SkuDAO skuDAO;

    @Autowired
    private SkuAttrDAO skuAttrDAO;

    @Autowired
    private SkuImgDAO skuImgDAO;

    /**
     * 分页查询
     * @param commonQueryParamBean
     * @return
     */
    public Page<Sku> query(CommonQueryParamBean commonQueryParamBean) {
        com.github.pagehelper.Page page = PageHelper.startPage(commonQueryParamBean.getPageNo(), commonQueryParamBean.getPageSize());
        List<Sku> skus = skuDAO.queryAttrs();

        return new Page(page.getPageNum(), page.getPageSize(), (int)page.getTotal(), skus);
    }

    /**
     * 新增
     * @param skuParamBean
     */
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public void create(SkuParamBean skuParamBean) {
        //新增
        Sku sku = BeanUtil.copy(skuParamBean, Sku.class);
        sku.setStatus(2);
        sku.setCreateTime(System.currentTimeMillis());
        sku.setUpdateTime(System.currentTimeMillis());

        skuDAO.insert(sku);

        saveAttr(sku.getId(), skuParamBean.getAttrId(), false);

        saveImg(sku.getId(), skuParamBean.getImgs(), false);
    }

    /**
     * 修改
     * @param skuParamBean
     */
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public void update(SkuParamBean skuParamBean) {
        Sku currentSku = skuDAO.selectByPrimaryKey(skuParamBean.getId());
        if(StringUtil.isEmpty(currentSku)) {
            throw new BizException(ResponseCode.SKU_UN_EXIST, "sku不存在");
        }
        //修改
        Sku sku = BeanUtil.copy(skuParamBean, Sku.class);
        sku.setStatus(null);
        sku.setCreateTime(null);
        sku.setUpdateTime(System.currentTimeMillis());

        skuDAO.updateByPrimaryKeySelective(sku);

        saveAttr(sku.getId(), skuParamBean.getAttrId(), true);

        saveImg(sku.getId(), skuParamBean.getImgs(), true);
    }

    /**
     * 启用
     * @param id
     */
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public void use(Long id) {
        Sku sku = skuDAO.selectByPrimaryKey(id);
        if(StringUtil.isEmpty(sku)) {
            throw new BizException(ResponseCode.SKU_UN_EXIST, "sku不存在");
        }
        if(1 == sku.getStatus()) {
            throw new BizException(ResponseCode.SKU_STATUS_ERROR, "sku状态错误");
        }
        sku.setStatus(1);
        skuDAO.updateByPrimaryKeySelective(sku);
    }

    /**
     * 停用
     * @param id
     */
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public void cancel(Long id) {
        Sku sku = skuDAO.selectByPrimaryKey(id);
        if(StringUtil.isEmpty(sku)) {
            throw new BizException(ResponseCode.SKU_UN_EXIST, "sku不存在");
        }
        if(2 == sku.getStatus()) {
            throw new BizException(ResponseCode.SKU_STATUS_ERROR, "sku状态错误");
        }
        sku.setStatus(2);
        skuDAO.updateByPrimaryKeySelective(sku);
    }

    /**
     * 详情
     * @param id
     * @return
     */
    public Sku detail(Long id) {
        Sku sku = skuDAO.selectByPrimaryKey(id);
        if(StringUtil.isEmpty(sku)) {
            throw new BizException(ResponseCode.SKU_UN_EXIST, "sku不存在");
        }
        return sku;
    }

    /**
     * 清除缓存
     */
    public void clear() {
    }

    /**
     * 保存属性
     * @param id
     * @param attrId
     * @param del
     */
    private void saveAttr(Long id, Long attrId, boolean del) {
        if(del) {
            SkuAttr skuAttr = new SkuAttr();
            skuAttr.setAttrId(attrId);
            skuAttr.setSkuId(id);
            skuAttrDAO.deleteByExample(skuAttr);
        }
        SkuAttr skuAttr = new SkuAttr();
        skuAttr.setAttrId(attrId);
        skuAttr.setSkuId(id);
        skuAttrDAO.insert(skuAttr);
    }

    /**
     * 保存图片
     * @param id
     * @param imgs
     * @param del
     */
    private void saveImg(Long id, List<String> imgs, boolean del) {
        if(del) {
            SkuImg skuImg = new SkuImg();
            skuImg.setSkuId(id);
            skuImg.setStatus(2);
            skuImgDAO.updateByPrimaryKeySelective(skuImg);
        }
        Long now = System.currentTimeMillis();
        imgs.forEach(img->{
            SkuImg skuImg = new SkuImg();
            skuImg.setSkuId(id);
            skuImg.setUrl(img);
            skuImg.setStatus(1);
            skuImg.setCreateTime(now);
            skuImgDAO.insert(skuImg);
        });
    }

}
